package tech.aistar.dao.impl;

import tech.aistar.dao.IVideoDao2;
import tech.aistar.model.pojo.Video;
import tech.aistar.util.JdbcTemplate;

import java.sql.PreparedStatement;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2022/8/12 10:50 上午
 */
public class VideoDaoImpl2 implements IVideoDao2 {
    @Override
    public List<Video> findByUserId(Integer userId) {
        return JdbcTemplate.queryList(Video.class,"select * from jdbc_video where user_id=?",userId);
    }

    @Override
    public List<Video> find(String title, Integer pageNum, Integer pageSize) {
        return JdbcTemplate.queryList(Video.class,"select * from jdbc_video where title like ? limit ?,?","%"+title+"%",(pageNum-1)*pageSize,pageSize);
    }

    @Override
    public List<Video> getAll() {
        return JdbcTemplate.getAll(Video.class);
    }

    @Override
    public Integer countRows(String title) {
        return (Integer) JdbcTemplate.executeQuery(conn->{
            String sql = "select count(*) from jdbc_video where title like ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setObject(1,"%"+title+"%");
            return pst;
        },rs->{
            Integer count = 0;
            if(rs.next()){
                count = rs.getInt(1);
            }
            return count;
        });
    }
}
